package com.saxon.shuttle.consumer.controller;

import com.saxon.shuttle.consumer.feign.PortRemote;
import com.saxon.shuttle.consumer.service.HelloService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

@RestController
public class IndexController {
    private final Logger log = LoggerFactory.getLogger(getClass());
    @Value("${server.port}")
    private String appPort;
    @Value("${spring.application.name}")
    private String appName;

    @Autowired
    private WebClient webClient;

    @Autowired
    private HelloService helloService;

    @Autowired
    private PortRemote portRemote;

    @RequestMapping(value = "/hello", method = RequestMethod.GET)
    public String hello(@RequestParam String name) {
        return helloService.hello(name);
    }

    @RequestMapping(value = "/hi", method = RequestMethod.GET)
    public String hi(@RequestParam String name) {
        return appName + ":Hi, " + name + "!";
    }

    @GetMapping("/port")
    public String port() {
        log.info("localPort:{}", appPort);
        return appPort;
    }

    @GetMapping("/getPort")
    public String getPort() {
        String remotePort = portRemote.getPort();
        log.info("portRemote:{}", remotePort);
        return remotePort;
    }

    @GetMapping("/tracer")
    public Mono<String> tracer() {
        return webClient.get()
                .uri("/trace")
                .retrieve()
                .bodyToMono(String.class);
    }

    @GetMapping("/trace")
    public Mono<String> trace() {
        return Mono.just("Trace from " + appName);
    }
}
